package com.qdxwx.web.controller.h5;


import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.data.service.course.CourseStudyHistoryService;
import com.qdxwx.models.course.CourseDTO;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.enums.CourseType;
import com.qdxwx.models.user.User;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 个人中心  Controller
 * @author developer
 */
@Controller
@RequestMapping("/user/course")
public class H5UserCourseController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(H5UserCourseController.class);

	@Autowired
	private CourseService courseService;
	@Autowired
	private CourseStudyHistoryService courseStudyHistoryService;
	/**
	 * 进入个人中心课程列表
	 */
	@RequestMapping("/list.html")
	public String courseInfo(HttpServletRequest request, ModelMap mmap) {
		User user = LoginUser.getUser(request);
		if (user == null) {
			return toLoginPage();
		}
		long userId = user.getUserId();
		CourseType courseType = CourseType.get(request.getParameter("courseType"), CourseType.COURSE);
		int isOverdue = TKConvert.toInteger(request.getParameter("isOverdue"));
		PageRequest page = new PageRequest();
		int currentPage = TKConvert.toInteger(request.getParameter("page"));
		page.setCurrentPage(currentPage);
		page.setPageSize(5);
		CourseQuery courseQuery = new CourseQuery();
		courseQuery.setUserId(userId);
		courseQuery.setSellType(courseType.toString());
		courseQuery.setIsOverdue(isOverdue);
		int total = courseService.countMyCourse(courseQuery);
		List<CourseDTO> courseList = courseService.getMyCourseList(courseQuery, page);
		if (courseList != null && courseList.size() > 0) {
			/*课程学习进度*/
			courseStudyHistoryService.setStudyPercent(courseList, userId);
			/*查询课程最近的一次 学习记录*/
			for (CourseDTO course : courseList) {
				course.setCourseStudyHistory(courseStudyHistoryService.getLastByUserId(userId, course.getCourseId()));
			}
		}
		mmap.put("totalPages", page.getTotalPages(total));
		mmap.put("courseType", courseType.toString());
		mmap.put("isOverdue", isOverdue);
		mmap.put("courseList", courseList);
		mmap.put("page", page);
		mmap.put("user", user);
		return getViewPathH5("user/uc-course");
	}

}